package oack1;

import java.io.*;

import java.net.*;
import java.sql.SQLException;
import java.util.ArrayList;

//import clientserver.clientThread;

public class MultiThreadChatServer{

	 // Declarat ion section:
    // declare a server socket and a client socket for the server
    // declare an input and an output stream
    
    static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

    // This chat server can accept up to 10 clients' connections

    static  clientThread t[] = new clientThread[10];           
    
    public static void main(String args[]) {
	
	// The default port

	int port_number=1003;
	
	if (args.length < 1)
	    {
		System.out.println("Usage: Nostalgia Server \n"+
				   "Now using port number="+port_number);
	    } else {
		port_number=Integer.valueOf(args[0]).intValue();
	    }
	
	// Initialization section:
	// Try to open a server socket on port port_number (default 2222)
	// Note that we can't choose a port less than 1023 if we are not
	// privileged users (root)

        try {
	    serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
	    {System.out.println(e);}
	
	// Create a socket object from the ServerSocket to listen and accept 
	// connections.
	// Open input and output streams for this socket will be created in 
	// client's thread since every client is served by the server in
	// an individual thread
	
	while(true){
	    try {
		clientSocket = serverSocket.accept();
		for(int i=0; i<=9; i++){
		    if(t[i]==null)
			{
			    (t[i] = new clientThread(clientSocket,t)).start();
			    break;
			}
		}
	    }
	    catch (IOException e) {
		System.out.println(e);}
	}
    }
} 

// This client thread opens the input and the output streams for a particular client,
// ask the client's name, informs all the clients currently connected to the 
// server about the fact that a new client has joined the chat room, 
// and as long as it receive data, echos that data back to all other clients.
// When the client leaves the chat room this thread informs also all the
// clients about that and terminates. 

class clientThread extends Thread{
    
    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;       
    clientThread t[]; 
    
    public clientThread(Socket clientSocket, clientThread[] t){
	this.clientSocket=clientSocket;
        this.t=t;
    }
    
    public void run() 
    {
	String line;
        String message;
	try{
	    is = new DataInputStream(clientSocket.getInputStream());
	    os = new PrintStream(clientSocket.getOutputStream());
	    
	    while(true){
	    	message=is.readLine(); 
	    	if(message.equals("sign up")){
	    	os.println("  Please Register");
	    	//os.println("Please Register Please");
	    	/*
	    	 * Problem here somewhere!!
	    	*/
	    	message=is.readLine();
	    	os.println("Message received was - " + message);
	    	if(message.startsWith("data")){
	    		System.out.println("Parsing Data");
	    		int res = StringParse.DetailParser(message);
	    		System.out.println(res);
	    		if (res==1){
	    		os.println("submitted");
	    		}
	    	
	    	else {
	    		os.println("Unsuccessful. Please Register again!\n");
	    	}
	    	}
	    	else{
	    		os.println("Wrong input");
	    		//System.exit(MAX_PRIORITY);
	    	}
	      }
	      
	      if (message.startsWith("sign in")){
	    	  os.println("authenticate");
	    	  message = is.readLine();
	    	  if(message.startsWith("secret")){
	    		  
	    		  int res = StringParse.LoginParser(message);
		    	  
	    		  if(res==1){
	    		  os.println("Success");
	    		 
	    	  }
	    	  else {
	    		  os.println("Failed. Reauthenticate");
	    	  }
	    	  }
	    	  
	      }
	      
	      if(message.startsWith("add")){
	    	  os.println("getDetails");
	    	  message = is.readLine();
	    	  int res = StringParse.EventParser(message);
	    	  
	    	  if(res==1){
	    		  os.println("Event Added");
	    	  }
	    	  else{
	    		  os.println("Event Adding Failed");	    		  
	    	  }
	      }
	      
	    
	     if(message.startsWith("query")){
	    	 os.println("Specify Tags to search");
	    	 message=is.readLine();
	    	 ArrayList<String> result = StringParse.TagParser(message);
	    	 for(int i=0;i<result.size();i++){
	    	 os.println(result.get(i));
	    	 }
	    	 }
	     if(message.equalsIgnoreCase("bye")){
	    	 System.exit(MAX_PRIORITY);
	     }
	    }
	    
	    
	    
	    
		

	    // Clean up:
	    // Set to null the current thread variable such that other client could
	    // be accepted by the server

	      
		
	    // close the output stream
	    // close the input stream
	    // close the socket

	}
	catch(IOException e){} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	};
    }
}